package com.youedata.modular.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youedata.base.pojo.page.LayuiPageFactory;
import com.youedata.base.tips.DCResponse;
import com.youedata.modular.entity.WaterMeterRepairInfo;
import com.youedata.modular.model.dto.WaterMeterRepairInfoDTO;
import com.youedata.modular.service.IWaterMeterRepairInfoService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 水表保修表控制器
 *
 * @author hst
 * @date 2021-07-26 18:38:30
 */
@Controller
@Api(value = "/waterMeterRepairInfo", tags = "水表保修表管理")
@RequestMapping("/waterMeterRepairInfo")
public class WaterMeterRepairInfoController {

    private String PREFIX = "/waterMeterRepairInfo";

    @Autowired
    private IWaterMeterRepairInfoService waterMeterRepairInfoService;

    /**
     * 新增
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/addItem")
    @ApiOperation(value = "新增")
    public DCResponse<Void> addItem(WaterMeterRepairInfoDTO waterMeterRepairInfoDto) {
        WaterMeterRepairInfo waterMeterRepairInfo = new WaterMeterRepairInfo();
        BeanUtils.copyProperties(waterMeterRepairInfoDto, waterMeterRepairInfo);
        waterMeterRepairInfo.setCreateUser(String.valueOf(UserHolder.getUserId()));
        waterMeterRepairInfo.setCreateTime(LocalDateTime.now());
        this.waterMeterRepairInfoService.save(waterMeterRepairInfo);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键删除
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/delete")
    @ApiOperation("删除")
    public DCResponse<Void> delete(@RequestParam("id") String id) {
        this.waterMeterRepairInfoService.removeById(id);
        return DCResponse.ok(null);
    }

    /**
     * 修改
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/editItem")
    @ApiOperation("根据ID修改信息")
    public DCResponse<Void> editItem(WaterMeterRepairInfoDTO waterMeterRepairInfoDto) {
        WaterMeterRepairInfo waterMeterRepairInfo = new WaterMeterRepairInfo();
        BeanUtils.copyProperties(waterMeterRepairInfoDto, waterMeterRepairInfo);
        waterMeterRepairInfo.setUpdateUser(String.valueOf(UserHolder.getUserId()));
        waterMeterRepairInfo.setUpdateTime(LocalDateTime.now());
        this.waterMeterRepairInfoService.updateById(waterMeterRepairInfo);
        return DCResponse.ok(null);
    }

//    /**
//     *
//     * 前端传入数据存储
//     *
//     *
//     */
//    @PostMapping("/releaseSignedSave")
//    public Result releaseSignedSave(@ResponseBody )
    /**
     * 根据主键查询单条详情
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键查询单条详情")
    public ResponseData detail(@RequestParam("id") String id) {
        return ResponseData.success(this.waterMeterRepairInfoService.getById(id));
    }

    /**
     * 分页查询列表
     *
     * @author hst
     * @date 2021-07-26
     */
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码", required = true, defaultValue = "1", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, defaultValue = "10", paramType = "query", dataType = "int", example = "20")
    })
    public Object list(WaterMeterRepairInfoDTO waterMeterRepairInfoDto) {
        WaterMeterRepairInfo waterMeterRepairInfo = new WaterMeterRepairInfo();

        //过滤空字符串
        waterMeterRepairInfoDto.setName(StringUtils.isEmpty(waterMeterRepairInfoDto.getName())?null:waterMeterRepairInfoDto.getName());
        waterMeterRepairInfoDto.setTag(StringUtils.isEmpty(waterMeterRepairInfoDto.getTag())?null:waterMeterRepairInfoDto.getTag());

        BeanUtils.copyProperties(waterMeterRepairInfoDto, waterMeterRepairInfo);

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //根据条件查询
        //page.setRecords(this.waterMeterRepairInfoService.page(page, Wrappers.query(waterMeterRepairInfo)).getRecords());
       // page.setRecords(this.waterMeterRepairInfoService.queryFuzzySearch(page,waterMeterRepairInfo));
        List<WaterMeterRepairInfo> waterMeterRepairInfoList = this.waterMeterRepairInfoService.queryFuzzySearch(page, waterMeterRepairInfo);
        if (ObjectUtils.isNotEmpty(waterMeterRepairInfoList)) {
            waterMeterRepairInfoList.stream().map(x -> {
                if (ToolUtil.isNotEmpty(x.getCreateTime())) {
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                    x.setCreateTimeStr(dtf.format(x.getCreateTime()));
                }
                return x;
            }).collect(Collectors.toList());
        }
        page.setRecords(waterMeterRepairInfoList);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping()
    public String index() {
        return PREFIX + "/waterMeterRepairInfo.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/waterMeterRepairInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-26
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/waterMeterRepairInfo_edit.html";
    }

}
